package com.example.bilibili_miaosha.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.bilibili_miaosha.entity.User;
import com.example.bilibili_miaosha.mapper.UserMapper;
import com.example.bilibili_miaosha.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bilibili_miaosha.utils.CookieUtil;
import com.example.bilibili_miaosha.utils.MD5Util;
import com.example.bilibili_miaosha.utils.UUIDUtil;
import com.example.bilibili_miaosha.utils.ValidatorUtil;
import com.example.bilibili_miaosha.vo.LoginVo;
import com.example.bilibili_miaosha.vo.RespBean;
import com.example.bilibili_miaosha.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
* 弓川hello mybatisplus
 *
 * @author gongchuan
 * @since 2022-03-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
    * @Description: 登录逻辑
    * @Param:  手机号and密码
    * @return:  公共返回对象code，msg，obj
    * @Author: 弓川
    * @Date: 2022/3/28
    **/
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("调用了doLogin");
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        //非空判断
//        if (mobile.isEmpty()||password.isEmpty()) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        //手机号判断
//        if (!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        //根据手机号获取用户
        User user = userMapper.selectById(mobile);
        if (user==null){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if (!MD5Util.formPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        //生成cookie
        String ticket  = UUIDUtil.uuid();
        System.out.println("ticket:"+ticket);
//        Cookie cookie = new Cookie("userTicket",ticket);
//        response.addCookie(cookie);
//        request.getSession().setAttribute(ticket,user);//设置session,将用户对象存储到session中s（最基础版本的登录session）
    //将用户存储到redis中
        redisTemplate.opsForValue().set("user:"+ticket,user);

        CookieUtil.setCookie(request,response,"userTicket",ticket);

        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String ticket,HttpServletRequest request,HttpServletResponse response) {
        if (StringUtils.isEmpty(ticket)) {
            return null;
        }
        System.out.println("调用了getUserByCookie!");
        User user = (User) redisTemplate.opsForValue().get("user:" + ticket);
        if (user != null) {//就是一个以防万一的操作
//            System.out.println("调用了getUserByCookie！！");
            CookieUtil.setCookie(request,response,"userTicket",ticket);
        }

        return user;
    }


    @Override
    public User getByUserTicket(String ticket, HttpServletRequest request, HttpServletResponse response) {
        User user = (User) redisTemplate.opsForValue().get("user:" + ticket);

        return user;
    }


}
